package com.sx.admin.controller.plat;

import com.sx.admin.controller.BaseController;
import com.sx.core.mybatis.SessionTemplate;
import com.sx.core.utils.UUIDUtils;
import com.sx.core.vo.JsonReaderResponse;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.Map;

/**
 * Created by lihao on 16/12/21.
 */
@RestController
@RequestMapping("pl/content")
public class ContentController extends BaseController {
    @Autowired
    SessionTemplate sessionTemplate;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping(value = "list")
    public JsonReaderResponse list() {
        return new JsonReaderResponse(jdbcTemplate.queryForList("SELECT * FROM t_sx_content ORDER BY createtime DESC "));

    }
    @RequestMapping(value = "save")
    public JsonReaderResponse uploadPartScore(@RequestParam Map<String,Object> params) {

        String id= ObjectUtils.toString(params.get("id"));
        String no= ObjectUtils.toString(params.get("no"));
        if(StringUtils.isEmpty(id)){
            long count=jdbcTemplate.queryForObject("SELECT count(*) FROM t_sx_content WHERE no=?",Long.class,no);
            if(count>0){
                return new JsonReaderResponse("编号已存在",101);
            }

            params.put("id", UUIDUtils.generateUuid32());
            params.put("createtime",new Date());
            namedParameterJdbcTemplate.update("insert into t_sx_content (id,title,createtime,no) values (:id,:title,:createtime,:no);",params);
        }else{
            long count=jdbcTemplate.queryForObject("SELECT count(*) FROM t_sx_content WHERE no=? AND id!=?",Long.class,no,id);
            if(count>0){
                return new JsonReaderResponse("编号已存在",101);
            }
            namedParameterJdbcTemplate.update("update t_sx_content set  title=:title,no=:no where id=:id",params);
        }
        return new JsonReaderResponse();

    }
    @RequestMapping(value = "delete")
    public JsonReaderResponse uploadAlltScore( String id) {
        jdbcTemplate.update("DELETE FROM t_sx_content WHERE id=?",id);
        return new JsonReaderResponse();

    }

    @RequestMapping(value = "contents")
    public ModelAndView labs() {
        return new ModelAndView("plat/content/contents");
    }
    @RequestMapping(value = "update")
    public ModelAndView update(String id,Model model) {
        if(StringUtils.isNotBlank(id)){
            model.addAttribute("command",jdbcTemplate.queryForMap("SELECT * FROM t_sx_content WHERE id=?",id));
        }
        return new ModelAndView("plat/content/update");
    }








}
